﻿<div style="width: 100%;">
    <div class="mini-toolbar" style="border-bottom: 0; padding: 0px;">
        <table style="width: 100%;">
            <tr>
                <td style="width: 100%;">
                    <a id="add" class="mini-button" iconcls="icon-add" plain="true" onclick="addRow()">增加</a>
                    <a id="delete" class="mini-button" iconcls="icon-remove" plain="true" onclick="removeRow()">删除</a>
                    <a id="edit" class="mini-button" iconcls="icon-save" plain="true" onclick="saveData()">保存</a>
                </td>
                <td style="white-space: nowrap;">
                    <input id="key" class="mini-textbox" emptytext="请输入关键字" style="width: 250px;" onenter="onKeyEnter" />
                    <a class="mini-button" plain="true" onclick="search()">查询</a>
                </td>
            </tr>
        </table>
    </div>
</div>




<div class="mini-fit" style="height: 100%;">

    <div id="datagrid1" class="mini-datagrid" style="width: 100%; height: 100%;"
        idfield="id" pagesize="20" url="Get"
        allowcelledit="true" allowcellselect="true" multiselect="true"
        editnextonenterkey="true" editnextrowcell="true">
        <div property="columns">
            <div type="indexcolumn" width="25px"></div>
            <div type="checkcolumn" width="25px"></div>
            <div field="CityPriceID" visible="false"></div>
            <div field="StartCityID" width="70px" vtype="required" renderer="onrenderer">
                出发城市
                 <input property="editor" class="mini-treeselect" allowinput="false" valuefield="YID" displayfield="YName" textfield="YName" parentfield="FID"
                     url="/SystemData/City/Get" popupwidth="255px" popuheight="350px" virtualscroll="true" multiselect="false" />
            </div>
            <div field="EndCityID" width="70px" vtype="required" renderer="onrenderer">
                目的城市
               <input property="editor" class="mini-treeselect" allowinput="false" valuefield="YID" displayfield="YName" textfield="YName" parentfield="FID"
                   url="/SystemData/City/Get" popupwidth="255px" popuheight="350px" virtualscroll="true" multiselect="false" />
            </div>
            <div field="PerTon" width="70px" vtype="required; float;">
                每吨运费
                <input property="editor" class="mini-textbox" style="width: 100%;" />
            </div>
            <div field="PerStere" width="80px" vtype="required; float;">
                每立方米运费
                <input property="editor" class="mini-textbox" style="width: 100%;" />
            </div>
            <div field="PerUnit" width="70px" vtype="required; float;">
                每件运费
                <input property="editor" class="mini-textbox" />
            </div>
            <div field="Remark" width="100px">
                备注
                <input property="editor" class="mini-textarea" minheight="200px" />
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">

    var Genders;

    mini.parse();

    var grid = mini.get("datagrid1");
    grid.load();

    grid.on("preload", function () {
        if (!Genders)
            $.ajax({
                url: "/SystemData/City/Get",
                async: false,
                success: function (f) {
                    Genders = mini.decode(f);
                }
            });
    });

    function onrenderer(e) {
        for (var i = 0, l = Genders.length; i < l; i++) {
            var g = Genders[i];
            if (g.YID == e.value) return g.YName;
        }
        return "";
    }

    function search() {
        var key = mini.get("key").getValue();

        grid.load({
            key: key, fail: function () {
            }
        });
    }

    function onKeyEnter(e) {
        search();
    }

    function addRow() {
        var newRow = { name: "New Row" };
        grid.addRow(newRow, 0);
        grid.beginEditCell(newRow, "StartCityID");
    }

    function removeRow() {
        var rows = grid.getSelecteds();
        if (rows.length > 0) {
            grid.removeRows(rows, true);
        }
    }

    function saveData() {

        grid.validate();
        if (grid.isValid() == false) {
            var error = grid.getCellErrors()[0];
            grid.beginEditCell(error.record, error.column);
            return;
        }

        var added = grid.getChanges("added");
        var jsonAdd = mini.encode(added);

        var modified = grid.getChanges("modified");
        var jsonEdit = mini.encode(modified);

        var removed = grid.getChanges("removed");
        var jsonDel = mini.encode(removed);

        grid.loading("保存中，请稍后......");
        $.ajax({
            url: "Save",
            data: { add: jsonAdd, edit: jsonEdit, del: jsonDel },
            type: "post",
            success: function (text, flag, res) {
                grid.unmask();
                if (res.responseText == "") {
                    grid.reload();
                }
                else
                    top.mini.alert(res.responseText);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                grid.unmask();
                top.mini.alert(errorThrown.responseText);
            }
        });
    }


    grid.on("drawcell", function (e) {
        if ($.trim(e.column.header) == "内容") {
            e.cellHtml = "<a href=\"javascript:editNContent(); \">编辑内容</a>";
        }
    });

    function editNContent() {
        var _row = grid.getSelected();
        mini.open({
            url: "/SystemData/AD/Show",
            title: "公告内容",
            width: "800px",
            height: "420px",
            allowResize: true,
            showMaxButton: true,
            allowDrag: true,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { NContent: unescape(_row.NContent) };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                if (action == "ok") {
                    var iframe = this.getIFrameEl();
                    var data = iframe.contentWindow.GetData();
                    data = mini.clone(data);    //必须
                    grid.updateRow(_row, { NContent: escape(data) });
                }
            }
        });
    }


</script>



